package mc.contents.web.request;

import mc.contents.web.CrawConfig;
import mc.contents.web.SourceConfig;
import mc.contents.web.common.AbstractPostSender;
import mc.contents.web.common.SortMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 14-12-8.
 */
public class ReshelfPostSender extends AbstractPostSender {
    private static Logger log = LoggerFactory.getLogger( ReshelfPostSender.class );
    private ReshelfCrawRequest crawRequest;
    private Long id;
    private CrawConfig crawConfig;

    public ReshelfPostSender( ReshelfCrawRequest crawRequest, CrawConfig crawConfig, String configName, Long id ) {
        super( crawConfig, id, configName );
        this.crawRequest = crawRequest;
        this.id = id;
        this.crawConfig = crawConfig;
    }

    @Override
    public List<Task> buildTaskList() {
        int offset = crawRequest.getOffset();
        String sourceType = crawRequest.getSourceType();
        String sortType = crawRequest.getSortType();
        String sort = SortMap.getSortType( sortType );
        SourceConfig sourceConfig = SourceConfig.getSourceConfig( sourceType );
        String encodeKeyword = getEncodeKeyword( crawRequest.getKeyword(),sourceConfig.getCharset() );
        SimpleDateFormat format = new SimpleDateFormat( "yyyyMMdd" );
        Float minPrice = crawRequest.getMinPrice();
        if(minPrice==null){
            minPrice = 0f;
        }
        Float maxPrice = crawRequest.getMaxPrice();

        List<Task> taskList = new ArrayList<>( offset );
        for ( int i = 0; i < offset; i++ ) {
            Args args = new Args();
            args.setKeyword( encodeKeyword );
            args.setSort( sort );
            args.setS( String.valueOf( i * sourceConfig.getPageSize() ) );
            args.setId( String.valueOf( id ) );
            args.setCallbackUrl( crawConfig.getCallbackUrl() + "reshelfResponse" );
            args.setPageIndex( String.valueOf( i ) );
            args.setInitiativeId( format.format( new Date() ) + "&bcoffset=1" );
            if(maxPrice!=null){
                args.setMaxPrice( String.valueOf( maxPrice ) );
            }
            if(minPrice!=null){
                args.setMinPrice( String.valueOf( minPrice ) );
            }
            String hook = "function(args,result){return{url:args.callbackUrl+\"?id=\"+args.id+\"&pageIndex=\"+args.pageIndex+\"&minPrice=\"+args.minPrice+\"&maxPrice=\"+args.maxPrice,data:\"num_iid=\"+(result.itemId||[]).join(',')+\"&volume=\"+(result.volume||[]).join(',')+\"&sellerId=\"+(result.sellerId||[]).join(',')};}";
            args.setHook(hook);
            Task task = new Task();
            task.setArgs( args );
            task.setGuest_id( String.valueOf( id ) );
            taskList.add( task );
        }
        return taskList;
    }

    private String getEncodeKeyword( String keyword, String charset ) {
        String encodeKeyword = null;
        try {
            encodeKeyword = URLEncoder.encode( keyword,charset );
        } catch ( UnsupportedEncodingException e ) {
            log.error( keyword + " encode error " );
        }
        return encodeKeyword;
    }
}
